package com.weishaoying.serivce;

import com.google.common.collect.*;
import com.weishaoying.api.entity.User;
import com.weishaoying.api.enums.GenderEnum;
import com.weishaoying.api.service.UserThriftService;
import org.apache.thrift.TException;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author WeiShaoying
 * @date 2018/9/27
 */
public class UserServiceImpl implements UserThriftService {

    private Random random = new Random();

    @Override
    public User queryUserByGender(GenderEnum gender) throws TException {
        System.out.println("----queryUserByGender() gender = [" + gender + "]");
        User user = new User();
        user.setId(random.nextInt(10000));
        user.setName("Jack" + random.nextInt(100));
        user.setAge(random.nextInt(20) + 30);
        user.setGender(gender);
        return user;
    }

    @Override
    public List<User> queryUsersByIdList(List<Integer> ids) throws TException {
        System.out.println("----queryUsersByIdList() ids = [" + ids + "]");
        List<User> userList = Lists.newArrayList();
        for (Integer id : ids) {
            User user = new User();
            user.setId(id);
            user.setName("Mike-" + random.nextInt(100));
            user.setAge(random.nextInt(20) + 30);
            user.setGender(GenderEnum.GENDER_MALE);
            userList.add(user);
        }
        return userList;
    }

    @Override
    public List<User> queryUsersByMap(User user, Map<String, String> map) {
        System.out.println("map = " + map);

        Multimap<String, Integer> multimap =  ArrayListMultimap.create();
        ImmutableMap<String, Integer> immutableMap = ImmutableMap.of("a", 1, "b", 2, "c", 3, "d", 1);

        SetMultimap<String,Integer>setMultimap = Multimaps.forMap(immutableMap);

        HashMultimap<Integer, String> hashMultimap = Multimaps.invertFrom(setMultimap, HashMultimap.create());

        Map<String, Collection<Integer>> stringCollectionMap = multimap.asMap();
        return null;
    }
}
